package com.kk.miaosha.service.impl;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kk.miaosha.entity.User;
import com.kk.miaosha.entity.UserRole;
import com.kk.miaosha.exception.CustomException;
import com.kk.miaosha.mapper.UserMapper;
import com.kk.miaosha.mapper.UserRoleMapper;
import com.kk.miaosha.securityEntity.UserDetail;
import com.kk.miaosha.securityEntity.UserDetailService;
import com.kk.miaosha.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kk.miaosha.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author kk
 * @since 2020-09-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public User findOne(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("user_name",username));
    }

    //刷新令牌
    public String refreshToken(String oldToken){
        if(!jwtTokenUtil.isTokenExpired(oldToken)){
            return jwtTokenUtil.refreshToken(oldToken);
        }
        return null;
    }
}
